package com.matrix.merchant.controller;

import com.matrix.api.merchant.dto.MerchantApplicationDTO;
import com.matrix.api.merchant.dto.ShopTableDTO;
import com.matrix.api.merchant.dto.ShopUpdateDTO;
import com.matrix.api.merchant.vo.ShopTableVO;
import com.matrix.api.merchant.vo.ShopDetailVO;
import com.matrix.common.domain.Result;
import com.matrix.common.exception.BusinessException;
import com.matrix.merchant.service.MerchantService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author 有点甜
 * @since 2025/7/26
 */
@RestController
@RequiredArgsConstructor
public class MerchantController {

    private final MerchantService merchantService;

    @PostMapping("/app/merchant/apply")
    @PreAuthorize("hasAuthority('MERCHANT')")
    public Result<Void> applyForSettlement(@RequestHeader("X-User-ID") Long userId,
                                           @Valid @RequestBody MerchantApplicationDTO applicationDTO) {
        merchantService.applyForSettlement(userId, applicationDTO);
        return Result.success();
    }

    @GetMapping("/app/merchant/{shopId}")
    public Result<ShopDetailVO> getShopDetails(@PathVariable("shopId") Long shopId) {
        return Result.success(merchantService.getShopDetailsById(shopId));
    }

    @PutMapping("/merchant/shop")
    @PreAuthorize("hasAuthority('MERCHANT')")
    public Result<Void> updateShopInfo(@RequestHeader("X-User-ID") Long userId,
                                       @Valid @RequestBody ShopUpdateDTO shopUpdateDTO) {
        merchantService.updateShopInfo(userId, shopUpdateDTO);
        return Result.success();
    }

    @GetMapping("/merchant/shop")
    @PreAuthorize("hasAuthority('MERCHANT')")
    public Result<ShopDetailVO> getMyAndShopInfo(@RequestHeader("X-User-ID") Long userId) {
        return Result.success(merchantService.getMyAndShopInfo(userId));
    }

    @PostMapping("/merchant/table")
    @PreAuthorize("hasAuthority('MERCHANT')")
    public Result<ShopTableVO> createTable(@RequestHeader("X-User-ID") Long userId,
                                           @Valid @RequestBody ShopTableDTO tableDTO) {
        return Result.success(merchantService.createTable(userId, tableDTO));
    }

    @PutMapping("/merchant/table/{tableId}")
    @PreAuthorize("hasAuthority('MERCHANT')")
    public Result<ShopTableVO> updateTable(@RequestHeader("X-User-ID") Long userId,
                                           @PathVariable Long tableId,
                                           @Valid @RequestBody ShopTableDTO tableDTO) {
        return Result.success(merchantService.updateTable(userId, tableId, tableDTO));
    }

    @DeleteMapping("/merchant/table/{tableId}")
    @PreAuthorize("hasAuthority('MERCHANT')")
    public Result<Void> deleteTable(@RequestHeader("X-User-ID") Long userId, @PathVariable Long tableId) {
        merchantService.deleteTable(userId, tableId);
        return Result.success();
    }

    @GetMapping("/merchant/tables")
    @PreAuthorize("hasAuthority('MERCHANT')")
    public Result<List<ShopTableVO>> listTables(@RequestHeader("X-User-ID") Long userId) {
        return Result.success(merchantService.listTables(userId));
    }

    @PostMapping("/merchant/table/status/{tableId}")
    @PreAuthorize("hasAuthority('MERCHANT')")
    public Result<Void> updateTableStatus(@RequestHeader("X-User-ID") Long userId,
                                          @PathVariable Long tableId,
                                          @RequestBody Map<String, Integer> payload) {
        Integer status = payload.get("status");
        if (status == null) {
            throw new BusinessException("状态值不能为空");
        }
        merchantService.updateTableStatus(userId, tableId, status);
        return Result.success();
    }

    @PutMapping("/merchant/shop/photos")
    @PreAuthorize("hasAuthority('MERCHANT')")
    public Result<Void> updateShopPhotos(@RequestHeader("X-User-ID") Long userId,
                                         @RequestBody List<String> photoUrls) {
        merchantService.updateShopPhotos(userId, photoUrls);
        return Result.success();
    }

}
